# -*- coding: utf-8 -*-
from __future__ import print_function, division
#系统库
import json
#第三方库
import tornado.web
import tornado.template
#自定义库
import auth
import hall
import Serverlib

logger = Serverlib.logging.getLogger('SERVER')


#主体
class MainHandler(tornado.web.RequestHandler):
    def get(self, *args, **kwargs):
        """说明
            web处理器
            如果检查到已经登录获得的合法验证码，则返回游戏页面game.html
            如果检查到没有合法的验证码，则返回登录界面login.html
        """
        if len(args) > 0:
            if args[0] == "term":
                self.write("公司是大爷，人傻钱多速度来")
                self.finish()
                return
        cookie = self.get_cookie("agot_user_id")
        if cookie == None:
            self.render("login.html")
            return
        user = auth.get_online_userid(cookie)
        if user == None:
            self.render("login.html")
            return
        self.render("game.html")
        return


class UserHandler(tornado.web.RequestHandler):
    def get(self, *args, **kwargs):
        """说明
        """
        return

    def post(self, *args, **kwargs):
        """说明
        """
        func = None
        try:
            func = getattr(self, "post_" + args[0])
        except Exception:
            raise tornado.web.HTTPError(403)
        if func != None:
            func(*args, **kwargs)

    def post_reg(self, *args, **kwargs):
        username = self.get_argument("username")
        email = self.get_argument("email")
        password = self.get_argument("password")
        code, errormsg = auth.register(username, password, email)
        ret = {
            "code": code,
            "errormsg": errormsg
        }
        self.write(json.dumps(ret))

    def post_login(self, *args, **kwargs):
        username = self.get_argument("username")
        password = self.get_argument("password")
        u_id, code, errormsg = hall.gamehall.player_login(username, password)
        if code == 0:
            self.set_secure_cookie("agot_user_id", str(u_id))
        ret = {
            "code": code,
            "errormsg": errormsg
        }
        self.write(json.dumps(ret))


class GameHallHandler(tornado.web.RequestHandler):
    def get(self, *args, **kwargs):
        """说明
        """
        func = None
        try:
            func = getattr(self, "get_" + args[0])
        except Exception:
            raise tornado.web.HTTPError(403)
        if func != None:
            func(*args, **kwargs)

    def post(self, *args, **kwargs):
        """说明
        """
        func = None
        try:
            func = getattr(self, "post_" + args[0])
        except Exception:
            raise tornado.web.HTTPError(403)
        if func != None:
            func(*args, **kwargs)

    def get_room(self, *args, **kwargs):
        name = self.get_argument("roomname")
        data, code, errormsg = hall.gamehall.get_room_info(name)
        ret = {
            "data": data,
            "code": code,
            "errormsg": errormsg
        }
        self.write(json.dumps(ret))

    def get_rooms(self, *args, **kwargs):
        roomlist = hall.gamehall.get_room_list()
        self.write(json.dumps(roomlist))

    def get_leave(self, *args, **kwargs):
        u_id = long(self.get_secure_cookie("agot_user_id"))
        code, errormsg = hall.gamehall.exit_room(u_id)
        ret = {
            "code": code,
            "errormsg": errormsg
        }
        self.write(json.dumps(ret))

    def get_ready(self, *args, **kwargs):
        u_id = long(self.get_secure_cookie("agot_user_id"))
        code, errormsg = hall.gamehall.player_ready(u_id)
        ret = {
            "code": code,
            "errormsg": errormsg
        }
        self.write(json.dumps(ret))

    def get_start(self, *args, **kwargs):
        u_id = long(self.get_secure_cookie("agot_user_id"))
        code, errormsg = hall.gamehall.game_start(u_id)
        ret = {
            "code": code,
            "errormsg": errormsg
        }
        self.write(json.dumps(ret))

    def post_create(self, *args, **kwargs):
        u_id = long(self.get_secure_cookie("agot_user_id"))
        name = self.get_argument("roomname")
        passwd = self.get_argument("password", None)
        code, errormsg = hall.gamehall.create_room(u_id, name, passwd)
        ret = {
            "code": code,
            "errormsg": errormsg
        }
        self.write(json.dumps(ret))

    def post_join(self, *args, **kwargs):
        u_id = long(self.get_secure_cookie("agot_user_id"))
        name = self.get_argument("roomname")
        passwd = self.get_argument("password", None)
        code, errormsg = hall.gamehall.join_room(u_id, name, passwd)
        ret = {
            "code": code,
            "errormsg": errormsg
        }
        self.write(json.dumps(ret))


class GameHandler(tornado.web.RequestHandler):
    def get(self, *args, **kwargs):
        """说明
        """
        func = None
        try:
            func = getattr(self, "get_" + args[0])
        except Exception:
            raise tornado.web.HTTPError(403)
        if func != None:
            func(*args, **kwargs)

    def post(self, *args, **kwargs):
        """说明
        """
        func = None
        try:
            func = getattr(self, "post_" + args[0])
        except Exception:
            raise tornado.web.HTTPError(403)
        if func != None:
            func(*args, **kwargs)

    def get_allstate(self, *args, **kwargs):
        u_id = long(self.get_secure_cookie("agot_user_id"))
        data, code, errormsg = hall.gamehall.get_game_info(u_id)
        ret = {
            "data": data,
            "code": code,
            "errormsg": errormsg
        }
        self.write(json.dumps(ret))
